package com.stef.saas.ssm.controller.ums;

import com.stef.saas.common.base.dto.BaseResult;
import com.stef.saas.common.base.enums.ErrorCodeEnum;
import com.stef.saas.common.base.exception.BusinessException;
import com.stef.saas.ssm.domain.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;


/**
 * 用户表
 *
 * @author stef
 * @date 2021-05-05
 */
@RestController
@RequestMapping("/ssm/user")
@Api(value = "/ssm/user", tags = "用户管理")
public class UserController {

    private static Map<Long, User> users = new HashMap(){
        {
            put(1,User.builder()
                    .id(1L)
                    .avatar("aaa")
                    .nickname("张三")
                    .phone("13012121212")
                    .username("zhangsan")
                    .build()
            );
            put(2,User.builder()
                    .id(2L)
                    .avatar("bbb")
                    .nickname("李四")
                    .phone("15112121212")
                    .username("lisi")
                    .build()
            );
            put(3,User.builder()
                    .id(3L)
                    .avatar("ccc")
                    .nickname("王五")
                    .phone("18912121212")
                    .username("王五")
                    .build()
            );
        }
    };



    /**
     * 通过id查询用户
     * @param id id
     * @return BaseResult
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public BaseResult getById(@PathVariable("id" ) Long id) {
        Optional<User> user = users.values().stream().filter(v -> v.getId().equals(id)).findFirst();
        if (!user.isPresent()){
            throw  new BusinessException(ErrorCodeEnum.UMS10001);
        }
        return BaseResult.ok(user.get());
    }



}
